#include <iostream>
#include <map>
#include <vector>
#include <iomanip>

using namespace std;

class Bridge {
private:
    map<int, int> macTable;

public:
    void processFrame(int srcAddr, int srcPort, int destAddr) {
        learnMAC(srcAddr, srcPort);

        int destPort = determineDestPort(destAddr);

        cout << "Frame: src=" << hex << setw(1) << srcAddr 
             << "(" << srcPort << ") -> dest=" << setw(1) << destAddr 
             << " | Forward to port: ";
        
        if (destPort == -1) {
            cout << "all (flood)";
        } else {
            cout << destPort;
        }
        
        cout << " | MAC Table: ";
        printMacTable();
    }

private:
    void learnMAC(int addr, int port) {
        if (addr != 0xF) {
            macTable[addr] = port;
        }
    }

    int determineDestPort(int destAddr) {
        if (destAddr == 0xF) {
            return -1;
        }
        
        auto it = macTable.find(destAddr);
        if (it != macTable.end()) {
            return it->second;
        } else {
            return -1;
        }
    }

    void printMacTable() {
        if (macTable.empty()) {
            cout << "empty";
            return;
        }
        
        for (const auto& entry : macTable) {
            cout << hex << entry.first << "->" << entry.second << " ";
        }
    }
};

int main() {
    Bridge bridge;
    
    cout << "Simulating Bridge (ports: 1, 2)\n";
    cout << "Input format: srcAddr srcPort destAddr (4-bit hex, 0xF=broadcast)\n";
    cout << "Enter 'q' to quit\n\n";
    
    while (true) {
        int srcAddr, srcPort, destAddr;
        cout << "> ";
        
        string input;
        getline(cin, input);
        if (input == "q") break;
        
        istringstream iss(input);
        if (!(iss >> hex >> srcAddr >> srcPort >> destAddr)) {
            cout << "Invalid input!\n";
            continue;
        }
        
        if (srcPort != 1 && srcPort != 2) {
            cout << "Error: Port must be 1 or 2\n";
            continue;
        }
        if (srcAddr < 0 || srcAddr > 0xF || destAddr < 0 || destAddr > 0xF) {
            cout << "Error: Address must be 0-0xF\n";
            continue;
        }
        
        bridge.processFrame(srcAddr, srcPort, destAddr);
    }
    
    return 0;
}